package com.lw.util;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;

public class GameUtil {

    /**
     * 加载图片
     * @param imgPath
     * @return
     */
    public static BufferedImage loadBufferedImage(String imgPath){
        try {
            return ImageIO.read(new FileInputStream(imgPath));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 超过范围取范围内的值
     * @param num
     * @param min
     * @param max
     * @return
     */
    public static int rangeNum(int num,int min,int max){
        num = Math.max(num,min);
        num = Math.min(num,max);
        return num;
    }

    //最高纪录文件
    static File recordFile = new File(System.getProperty("user.dir") + "/record.txt");

    /**
     * 用于获取保存的最高纪录
     *
     * @return
     */
    public static int getRecord() {
        if(recordFile.exists()) {
            try (BufferedReader bufferedReader = new BufferedReader(new FileReader(recordFile))) {
                int s = Integer.parseInt(bufferedReader.readLine());
                return s;
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }
        return 0;
    }

    /**
     * 保存记录
     *
     * @param record
     */
    public static void setRecord(String record) {
        try (FileWriter fileWriter = new FileWriter(recordFile)) {
            fileWriter.write(record);
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }

}
